package com.zerui.manager.accessauthorg.gatewayimpl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zerui.manager.accessauthorg.AccessAuthOrg;
import com.zerui.manager.accessauthorg.convertor.AccessAuthOrgConvert;
import com.zerui.manager.accessauthorg.entity.AccessAuthOrgDo;
import com.zerui.manager.accessauthorg.gateway.AccessAuthOrgGateWay;
import com.zerui.manager.accessauthorg.mapper.AccessAuthOrgMapper;
import com.zerui.manager.web.cmd.AccessAuthOrgBindCmd;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author wh
 * @create_time 2023-08-23 15:28
 */
@Component
public class AccessAuthOrgGateWayImpl implements AccessAuthOrgGateWay {

    @Resource
    private AccessAuthOrgMapper accessAuthOrgMapper;

    @Override
    public Map<Long, Long> getAuthIdMapByOrgIds(List<Long> orgIds) {
        LambdaQueryWrapper<AccessAuthOrgDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AccessAuthOrgDo::getOrgId,orgIds);
        List<AccessAuthOrgDo> authOrgDos = accessAuthOrgMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(authOrgDos)){
            return authOrgDos.stream()
                    .collect(Collectors.toMap(AccessAuthOrgDo::getOrgId,AccessAuthOrgDo::getAccessAuthId));
        }
        return Collections.emptyMap();
    }

    @Override
    public void addBatch(List<AccessAuthOrg> accessAuthOrges) {
        List<AccessAuthOrgDo> accessAuthOrgDos = AccessAuthOrgConvert.INSTANCE.convertAccessAuthOrgsToAccessAuthOrgDos(accessAuthOrges);
        accessAuthOrgMapper.insertBatchSomeColumn(accessAuthOrgDos);
    }

	@Override
	public List<Long> deleteBatch(AccessAuthOrgBindCmd accessAuthOrgBindCmd) {
		LambdaQueryWrapper<AccessAuthOrgDo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(AccessAuthOrgDo::getAccessAuthId,accessAuthOrgBindCmd.getAccessAuthId());
		queryWrapper.in(AccessAuthOrgDo::getOrgId,accessAuthOrgBindCmd.getOrgIds());
		List<AccessAuthOrgDo> accessAuthOrgDos = accessAuthOrgMapper.selectList(queryWrapper);
		if(CollectionUtils.isNotEmpty(accessAuthOrgDos)){
			accessAuthOrgMapper.delete(queryWrapper);
			return accessAuthOrgDos.stream().map(AccessAuthOrgDo::getOrgId).collect(Collectors.toList());
		}
		return Collections.emptyList();
	}

    @Override
    public List<Long> deleteAccessAuthOrgById(Long id) {
        LambdaQueryWrapper<AccessAuthOrgDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccessAuthOrgDo::getAccessAuthId,id);
        List<AccessAuthOrgDo> accessAuthOrgDos = accessAuthOrgMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(accessAuthOrgDos)){
            accessAuthOrgMapper.delete(queryWrapper);
            return accessAuthOrgDos.stream().map(AccessAuthOrgDo::getOrgId).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public List<Long> getAccessAuthOrgById(Long accessAuthId) {
        LambdaQueryWrapper<AccessAuthOrgDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccessAuthOrgDo::getAccessAuthId,accessAuthId);
        List<AccessAuthOrgDo> accessAuthOrgDos = accessAuthOrgMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(accessAuthOrgDos)){
            return accessAuthOrgDos.stream().map(AccessAuthOrgDo::getOrgId).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public void deleteByOrgId(List<Long> orgIds) {
        LambdaQueryWrapper<AccessAuthOrgDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AccessAuthOrgDo::getOrgId,orgIds);
        accessAuthOrgMapper.delete(queryWrapper);
    }
}
